package com.forum.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.forum.bean.Comments;
import com.forum.common.Result;
import com.forum.dto.CommentCreateRequest;
import com.forum.dto.CommentListDto;
import com.forum.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 写评论接口：录入文章ID、用户ID、评论内容
     * 请求示例：
     * {
     *   "articleId": 1,
     *   "userId": 2,
     *   "content": "这是一条评论"
     * }
     */
    @PostMapping("")
    public Result<Comments> addComment(@RequestBody CommentCreateRequest request) {
        return commentService.addComment(request);
    }

    /**
     * DELETE /api/comments/{id}
     * 删除指定评论的接口
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteComment(@PathVariable("id") Integer commentId) {
        return commentService.deleteComment(commentId);
    }


    /**
     * 获取指定用户的评论列表，返回每条评论包括评论内容、创建日期，
     * 以及关联文章的信息和评论者的头像、名称等信息，按时间倒序排列。
     */
    @GetMapping("/list")
    public Result<IPage<CommentListDto>> getCommentList(@RequestParam("userId") Integer userId,
                                                        @RequestParam(defaultValue = "1") int pageNum,
                                                        @RequestParam(defaultValue = "100") int pageSize) {
        return commentService.getCommentList(userId, pageNum, pageSize);
    }
}
